package com.bpc.cs.managed.bean;

import java.io.Serializable;
import java.util.Locale;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.bpc.cs.constant.AttributeName;
import com.bpc.cs.constant.Constant;

/**
 * @author   phan_tp
 * @datetime Nov 30, 2012 : 2:49:31 PM
 */
public class BaseBean implements Serializable {
	private static final long serialVersionUID = -1777571134223615593L;

	/**
	 * Contructor of BaseBean
	 */
	public BaseBean() {
		HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true);
		// Get local form browser to view message
		if (session.getAttribute(AttributeName.USR_LOCALE) == null) {
			// Get and set locale to session for each user
			Locale browserLocale = getRequest().getLocale();
			session.setAttribute(AttributeName.USR_LOCALE, browserLocale.getLanguage());
		}
	}

	public HttpSession getSession() {
		return (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true);
	}

	public HttpServletRequest getRequest() {
		return (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
	}

	/**
	 * Get anohter bean in JSF
	 * @param String beanName
	 * @return bean object
	 */
	public static Object getBean(String beanName, Class<?> classType){
		beanName = Constant.CROSS + Constant.L_BRACE + beanName + Constant.R_BRACE;
		FacesContext context = FacesContext.getCurrentInstance();
		return context.getApplication().evaluateExpressionGet(context, beanName, classType);
	}

}
